package com.boboPlanet.user.mq.consumer;

import com.boboPlanet.comm.context.LoginUserContext;
import com.boboPlanet.comm.entity.dto.LoginUserInfoDTO;
import com.boboPlanet.comm.rabbitmq.RabbitMqRoutingConfig;
import com.boboPlanet.user.mapper.UserOperationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/5/16
 * @Company planet
 **/
@Slf4j
@Component
public class GatewayLoginExpireConsume {

    @Resource
    private UserOperationMapper userOperationMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RabbitListener(queues = RabbitMqRoutingConfig.gateway_login_expire_queue)
    @Transactional(rollbackFor = Exception.class)
    public void process(LoginUserInfoDTO userInfo) {

        log.info("GatewayLoginExpireConsume data: {}",userInfo);

        userOperationMapper.clearUserLoginState(userInfo.getAccountId());

        stringRedisTemplate.delete(userInfo.getToken());

        LoginUserContext.clear();

    }
}
